<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="formData.date"
          type="date"
          value-format="x"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="粒度" prop="granularity">
        <el-input v-model="formData.granularity" placeholder="请输入粒度" />
      </el-form-item>
      <el-form-item label="总展示次数" prop="impressions">
        <el-input v-model="formData.impressions" placeholder="请输入总展示次数" />
      </el-form-item>
      <el-form-item label="总点击次数" prop="taps">
        <el-input v-model="formData.taps" placeholder="请输入总点击次数" />
      </el-form-item>
      <el-form-item label="总安装次数" prop="installs">
        <el-input v-model="formData.installs" placeholder="请输入总安装次数" />
      </el-form-item>
      <el-form-item label="总新安装" prop="newDownloads">
        <el-input v-model="formData.newDownloads" placeholder="请输入总新安装" />
      </el-form-item>
      <el-form-item label="总重复安装" prop="reDownloads">
        <el-input v-model="formData.reDownloads" placeholder="请输入总重复安装" />
      </el-form-item>
      <el-form-item label="LAT开安装次数" prop="latOnInstalls">
        <el-input v-model="formData.latOnInstalls" placeholder="请输入LAT开安装次数" />
      </el-form-item>
      <el-form-item label="LAT关安装次数" prop="latOffInstalls">
        <el-input v-model="formData.latOffInstalls" placeholder="请输入LAT关安装次数" />
      </el-form-item>
      <el-form-item label="总点击率" prop="ttr">
        <el-input v-model="formData.ttr" placeholder="请输入总点击率" />
      </el-form-item>
      <el-form-item label="每次转化费用" prop="avgCpaAmount">
        <el-input v-model="formData.avgCpaAmount" placeholder="请输入每次转化费用" />
      </el-form-item>
      <el-form-item label="转化货币" prop="avgCpaCurrency">
        <el-input v-model="formData.avgCpaCurrency" placeholder="请输入转化货币" />
      </el-form-item>
      <el-form-item label="每次点击付费" prop="avgCptAmount">
        <el-input v-model="formData.avgCptAmount" placeholder="请输入每次点击付费" />
      </el-form-item>
      <el-form-item label="点击货币" prop="avgCptCurrency">
        <el-input v-model="formData.avgCptCurrency" placeholder="请输入点击货币" />
      </el-form-item>
      <el-form-item label="每千展费用" prop="avgCpmAmount">
        <el-input v-model="formData.avgCpmAmount" placeholder="请输入每千展费用" />
      </el-form-item>
      <el-form-item label="展示货币" prop="avgCpmCurrency">
        <el-input v-model="formData.avgCpmCurrency" placeholder="请输入展示货币" />
      </el-form-item>
      <el-form-item label="总支出金额" prop="localSpendAmount">
        <el-input v-model="formData.localSpendAmount" placeholder="请输入总支出金额" />
      </el-form-item>
      <el-form-item label="总支出货币" prop="localSpendCurrency">
        <el-input v-model="formData.localSpendCurrency" placeholder="请输入总支出货币" />
      </el-form-item>
      <el-form-item label="总转化率" prop="conversionRate">
        <el-input v-model="formData.conversionRate" placeholder="请输入总转化率" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CampaignGrandTotalsApi, CampaignGrandTotalsVO } from '@/api/asasc/campaignTotals'

/** 广告数据汇总 表单 */
defineOptions({ name: 'CampaignGrandTotalsForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  date: undefined,
  granularity: undefined,
  impressions: undefined,
  taps: undefined,
  installs: undefined,
  newDownloads: undefined,
  reDownloads: undefined,
  latOnInstalls: undefined,
  latOffInstalls: undefined,
  ttr: undefined,
  avgCpaAmount: undefined,
  avgCpaCurrency: undefined,
  avgCptAmount: undefined,
  avgCptCurrency: undefined,
  avgCpmAmount: undefined,
  avgCpmCurrency: undefined,
  localSpendAmount: undefined,
  localSpendCurrency: undefined,
  conversionRate: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CampaignGrandTotalsApi.getCampaignGrandTotals(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CampaignGrandTotalsVO
    if (formType.value === 'create') {
      await CampaignGrandTotalsApi.createCampaignGrandTotals(data)
      message.success(t('common.createSuccess'))
    } else {
      await CampaignGrandTotalsApi.updateCampaignGrandTotals(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    date: undefined,
    granularity: undefined,
    impressions: undefined,
    taps: undefined,
    installs: undefined,
    newDownloads: undefined,
    reDownloads: undefined,
    latOnInstalls: undefined,
    latOffInstalls: undefined,
    ttr: undefined,
    avgCpaAmount: undefined,
    avgCpaCurrency: undefined,
    avgCptAmount: undefined,
    avgCptCurrency: undefined,
    avgCpmAmount: undefined,
    avgCpmCurrency: undefined,
    localSpendAmount: undefined,
    localSpendCurrency: undefined,
    conversionRate: undefined
  }
  formRef.value?.resetFields()
}
</script>
